﻿using System;
using System.Globalization;
using System.Threading;
using Medianamik.Client;
using Medianamik.Core;
using NUnit.Framework;
using Tests.Integration.Medianamik;

namespace Tests.Integration_Medianamik
{
    [TestFixture]
    public class UrlDictionaryTests
    {
        [TestFixtureSetUp]
        public void TestFixtureSetUp()
        {
            TestUtils.TestFixtureSetUp();
        }

        [TestFixtureTearDown]
        public void TestFixtureTearDown()
        {
            TestUtils.TestFixtureTearDown();
        }

        [SetUp]
        public void SetUp()
        {
            TestUtils.SetUp();
        }

        [TearDown]
        public void TearDown()
        {
            TestUtils.TearDown();
        }

        [Test]
        public void CreateANewWebPageGenerateAnUrl()
        {
            Node newNode = TestUtils.CreateAndSaveNode(Constants.NodeTypes.ContentPage, Constants.Nodes.WebSite, "Test");
            // Vérifier l'UrlDictionary
            string url = new Utility().GetDefaultRelativePath(newNode.NodeId, CultureInfo.CurrentUICulture);
            try
            {
                Assert.That(url, Is.EqualTo("/test.html"));
            }
            finally
            {
                // Effacer le noeud
                NodeManager.DestroyNode(newNode);
            }
        }

        [Test]
        public void CreateANewWebPageAndDeleteItShouldRemoveTheUrl()
        {
            Node newNode = TestUtils.CreateAndSaveNode(Constants.NodeTypes.ContentPage, Constants.Nodes.WebSite, "TestRemoveIt");
            // Vérifier l'UrlDictionary
            string url = new Utility().GetDefaultRelativePath(newNode.NodeId, CultureInfo.CurrentUICulture);
            try
            {
                Assert.That(url, Is.EqualTo("/testremoveit.html"));
            }
            finally
            {
                // Effacer le noeud
                NodeManager.DestroyNode(newNode);
            }
            // Vérifier l'UrlDictionary
            url = new Utility().GetDefaultRelativePath(newNode.NodeId, CultureInfo.CurrentUICulture);
            Assert.That(url, Is.EqualTo("#"));
        }

        [Test]
        public void CreateTwoNodeWithTheSameNameMustGenerateADifferentUrl()
        {
            Node newNode1 = TestUtils.CreateAndSaveNode(Constants.NodeTypes.ContentPage, Constants.Nodes.WebSite, "TestDuplicate");
            Node newNode2 = TestUtils.CreateAndSaveNode(Constants.NodeTypes.ContentPage, Constants.Nodes.WebSite, "TestDuplicate");
            try
            {
                // Vérifier l'UrlDictionary
                string url = new Utility().GetDefaultRelativePath(newNode1.NodeId, CultureInfo.CurrentUICulture);
                Assert.That(url, Is.EqualTo("/testduplicate.html"));
                url = new Utility().GetDefaultRelativePath(newNode2.NodeId, CultureInfo.CurrentUICulture);
                Assert.That(url, Is.Not.EqualTo("/testduplicate.html"));
            }
            finally
            {
                // Effacer le noeud
                NodeManager.DestroyNode(newNode1);
                NodeManager.DestroyNode(newNode2);
            }
        }

        [Test]
        public void CreateAndDeleteNodeShouldNotBreakDictionaryForFutureCreates()
        {
            Node newNode = TestUtils.CreateAndSaveNode(Constants.NodeTypes.ContentPage, Constants.Nodes.WebSite, "Test");
            // Vérifier l'UrlDictionary
            string url = new Utility().GetDefaultRelativePath(newNode.NodeId, CultureInfo.CurrentUICulture);
            try
            {
                Assert.That(url, Is.EqualTo("/test.html"));
            }
            finally
            {
                // Effacer le noeud
                NodeManager.DestroyNode(newNode);

            }

            Node newNode2 = TestUtils.CreateAndSaveNode(Constants.NodeTypes.ContentPage, Constants.Nodes.WebSite, "Test2");
            // Vérifier l'UrlDictionary
            string url2 = new Utility().GetDefaultRelativePath(newNode2.NodeId, CultureInfo.CurrentUICulture);
            try
            {
                Assert.That(url2, Is.EqualTo("/test2.html"));
            }
            finally
            {
                // Effacer le noeud
                NodeManager.DestroyNode(newNode2);
            }
        }

        [Test]
        public void CreateDeleteAndRecreateANodeShouldUseTheSameUrl()
        {
            Node newNode = TestUtils.CreateAndSaveNode(Constants.NodeTypes.ContentPage, Constants.Nodes.WebSite, "TestUrl");
            string url = new Utility().GetDefaultRelativePath(newNode.NodeId, CultureInfo.CurrentUICulture);
            NodeManager.DestroyNode(newNode);
            Node newNode2 = null;
            try
            {
                newNode2 = TestUtils.CreateAndSaveNode(Constants.NodeTypes.ContentPage, Constants.Nodes.WebSite, "TestUrl");
                string url2 = new Utility().GetDefaultRelativePath(newNode2.NodeId, CultureInfo.CurrentUICulture);
                Assert.That(url, Is.EqualTo(url2));
            }
            finally
            {
                if (newNode2 != null)
                    NodeManager.DestroyNode(newNode2);
            }
        }

        [Test]
        public void AddANodeInEnglishShouldWork()
        {
            Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-ca"); 
            Node newNode = TestUtils.CreateAndSaveNode(Constants.NodeTypes.ContentPage, Constants.Nodes.WebSite, "TestAddEnCa");
            try
            {
                string url = new Utility().GetDefaultRelativePath(newNode.NodeId, CultureInfo.CurrentUICulture);
                Assert.That(url, Is.EqualTo("/testaddenca.html"));
            }
            finally 
            {
                NodeManager.DestroyNode(newNode);
            }
        }

        [Test]
        public void AddingTheSameNodeNameInAnotherLanguageShouldAddTheLanguageInUrl()
        {
            Node newNode = TestUtils.CreateAndSaveNode(Constants.NodeTypes.ContentPage, Constants.Nodes.WebSite, "TestCultureAdd");
            string url = new Utility().GetDefaultRelativePath(newNode.NodeId, CultureInfo.CurrentUICulture);
            Node newNode2 = null;
            try
            {
                Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-ca");
                newNode2 = TestUtils.CreateAndSaveNode(Constants.NodeTypes.ContentPage, Constants.Nodes.WebSite, "TestCultureAdd");
                string url2 = new Utility().GetDefaultRelativePath(newNode2.NodeId, CultureInfo.CurrentUICulture);
                Assert.That(url2, Is.EqualTo("/testcultureadd-en-ca.html"));
            }
            finally
            {
                Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("fr-ca");
                if (newNode2 != null)
                    NodeManager.DestroyNode(newNode2);
                NodeManager.DestroyNode(newNode);
            }
        }

        // Créer un noeud en draft génère un url
        [Test]
        public void CreatingADraftNodeShouldGenerateAnUrl()
        {
            Node newNode = TestUtils.CreateAndSaveNode(Constants.NodeTypes.ContentPage, Constants.Nodes.WebSite, "TestCultureAdd", false);
            try
            {
                string url = new Utility().GetDefaultRelativePath(newNode.NodeId, CultureInfo.CurrentUICulture);
                Assert.That(url, Is.Not.Null);
                Assert.That(url, Is.Not.EqualTo(String.Empty));
            }
            finally
            {
                NodeManager.DestroyNode(newNode);
            }
        }
    }
}
